<template>
  <div>
    <el-table style="width: 100%" border :data="records">
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column prop="skuName" label="名称"> </el-table-column>
      <el-table-column prop="skuDesc" label="描述"> </el-table-column>
      <el-table-column prop="prop" label="默认图片" width="110">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80"> </el-table-column>
      <el-table-column prop="price" label="价格" width="80"> </el-table-column>
      <el-table-column prop="prop" label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            icon="el-icon-bottom"
            size="mini"
            v-if="row.isSale != 1"
            @click="cance(row)"
            title="下架商品"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-top"
            size="mini"
            v-else
            @click="sale(row)"
            title="上架商品"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit"
            title="修改商品"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            title="查看商品"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            title="删除商品"
            @click="DelSku(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  
       
       -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total"
    >
    </el-pagination>

    <!-- 抽屉效果 -->
    <el-drawer :show-close="false" :visible.sync="drawer" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            v-for="attr in skuInfo.skuAttrValueList"
            :key="attr.id"
            style="margin: 10px"
            >{{ attr.attrId }}-{{ attr.skuId }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">
          <el-carousel height="440px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="" style="width:100%;hegiht:100%">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 10,
      //存储列表数据
      records: [],
      total: 0,
      skuInfo: {},
      //控制显示与隐藏抽屉
      drawer: false,
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    async getSkuList() {
      //解构两个参数
      const { page, limit } = this;
      let reslut = await this.$API.sku.reqSkuList(page, limit);

      if (reslut.code == 200) {
        this.records = reslut.data.records;
        this.total = reslut.data.total;
      }
    },
    handleCurrentChange(pages) {
      this.page = pages;
      this.getSkuList();
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    //控制上架与下架
    async sale(row) {
      let reslut = await this.$API.sku.reqSale(row.id);
      if (reslut.code == 200) {
        this.$message({
          type: "success",
          message: "上架成功",
        });
        row.isSale = 0;
      }
    },
    async cance(row) {
      let reslut = await this.$API.sku.reqcancelSale(row.id);
      if (reslut.code == 200) {
        this.$message({
          type: "success",
          message: "下架成功",
        });
        row.isSale = 1;
      }
    },
    async getSkuInfo(row) {
      this.drawer = true;
      let reslut = await this.$API.sku.reqSkuById(row.id);
      if (reslut.code == 200) {
        this.skuInfo = reslut.data;
      }
    },
    edit() {
      this.$message({
        type: "warning",
        message: "正在开发中",
      });
    },
    async DelSku(row) {
      let reslut = await this.$API.sku.reqDelSku(row.id)
      if(reslut.code == 200) {
        this.$message({
          type:'success',
          message:'删除成功'
        })
        this.getSkuList()
      }
    }
  },
};
</script>

<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
  .el-carousel__button {
    width: 10px;
    height: 10px;
 
    border-radius: 50%;
  }
</style>
<style scoped>
.el-row .el-col-5 {
  font-size: 19px;
  text-align: right;
  font-weight: 700;
}
.el-col {
  margin: 10px;
}
</style>
